<?php
/**
 * 测试
 *
 * @author 孙晓晔
 * @version $Id: Js.class.php 998 2010-03-12 01:40:44Z sunxy $
 */
require_once(dirname(__FILE__) . '/LogUtil.class.php');

class Js extends LogUtil {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function a($controller, $request) {
		logdate($controller, $request);
		return true;
	}

	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function b($controller, $request) {

		$controller->display($request, 'system/js/b');
		return true;
	}
	
/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function validate($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'test/js/validate');
		return true;
	}

	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function bPost($controller, $request) {

		print_r($request);
		
		echo "OK.";
		
		
		$controller->display($request, 'test/js/b');
		return true;
	}



	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function p($controller, $request) {

		$controller->display($request, 'test/js/p');
		return true;
	}

	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function call($controller, $request) {

		$exten = $request['exten'];

		$aa['Action'] = 'OriginateCall';
		$aa['Type'] = 'default';
		$aa['Source'] = 'SIP/' . $exten;
		$aa['Destination'] = $request['number'];

		$json = json_encode($aa);

		$this->_call($json);

		$controller->display($request, 'test/js/p');
		return true;
	}

	function _call($json) {
		$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		if ($sock === false) {
			$error  = "!! Monast Error !!\n\nCould not create socket!\n";
			$error .= "reason: " . socket_strerror(socket_last_error());
			echo $json->encode(array(array('Action' => 'Error', 'Message' => $error)));
			die;
		}

		$conn = socket_connect($sock, "192.168.2.245", 5039);
		if ($conn === false) {
			$error  = "!! MonAst ERROR !!\n\nCould not connect to " . HOSTNAME . ":" . HOSTPORT . ": " . socket_strerror(socket_last_error()) . "\n";
			$error .= "Make sure monast.py is running so the panel can connect to its port properly.";
			echo $json->encode(array(array('Action' => 'Error', 'Message' => $error)));
			die;
		}

		socket_write($sock, "SESSION: " . time());
		socket_read($sock, 1024 * 16);
		socket_write($sock, $json . "\r\n");
		socket_write($sock, "BYE");

		socket_close($sock);
	}

	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function t($controller, $request) {


		$controller->display($request, 't');
		return true;
	}


}
?>
